package java.hilyb.dbtest;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * User: wlai
 * Date: 12/24/2014
 * Time: 15:22 PM
 */
public class ShortcutDemo extends Activity {
    private Button createBtn;
    private Button removeBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shortcut_demo);
        createBtn = (Button) findViewById(R.id.createBtn);
        removeBtn = (Button) findViewById(R.id.removeBtn);
        createBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ComponentName comp = new ComponentName(ShortcutDemo.this.getPackageName(), "."
                        + ShortcutDemo.this.getLocalClassName());
                Intent shortCutInt = new Intent(getApplicationContext(),
                        ShortcutDemo.class);
                shortCutInt.setAction(Intent.ACTION_MAIN).setComponent(comp);
                Intent addInt = new Intent();
                addInt.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutInt)
                        .setComponent(comp);
                addInt.putExtra(Intent.EXTRA_SHORTCUT_NAME, "dbtest");
                addInt.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                        Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                                R.drawable.ic_launcher));
                addInt.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
// Broadcast the created intent
                getApplicationContext().sendBroadcast(addInt);
            }
        });

        removeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent shortcutInt = new Intent(getApplicationContext(),
                        ShortcutDemo.class);
                shortcutInt.setAction(Intent.ACTION_MAIN);
                Intent addInt = new Intent();
                addInt.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutInt);
                addInt.putExtra(Intent.EXTRA_SHORTCUT_NAME, "dbtest");
                addInt.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
// Broadcast the created intent
                getApplicationContext().sendBroadcast(addInt);
            }
        });
    }
}